package com.jackeysun.common_kit

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.jackeysun.common.AbsAdapter
import com.jackeysun.common.AbsViewHolder
import com.jackeysun.common.IData
import com.jackeysun.common_kit.databinding.ActivityMain4Binding
import com.jackeysun.common_kit.databinding.FragmentTest2Binding
import com.jackeysun.common_kit.databinding.FragmentTestBinding
import com.jackeysun.common_kit.databinding.ItemInfoBinding

class MainActivity4 : AppCompatActivity() {

    val tabs = listOf("背单词", "历届真题", "备考计划", "考前评测")

    private lateinit var mBinding: ActivityMain4Binding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityMain4Binding.inflate(layoutInflater)
        setContentView(mBinding.root)

        mBinding.viewpager.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = tabs.size

            override fun createFragment(position: Int): Fragment {
                return when (position) {
                    0, 2, 3 -> TestFragment().apply {
                        val bundle = Bundle()
                        bundle.putInt("page", position)
                        arguments = bundle
                    }
                    else -> Test2Fragment()
                }
            }

        }

        TabLayoutMediator(mBinding.tab, mBinding.viewpager) { tab, position ->
            tab.text = tabs[position]
        }.attach()
    }
}


class TestFragment : Fragment() {
    private lateinit var mBinding: FragmentTestBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentTestBinding.inflate(inflater, container, false)
        return mBinding.root
    }

    val data = mutableListOf<TestData>()
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val page = arguments?.getInt("page") ?: 0

        for (i in 0..100){
            data.add(TestData("$i 。。。。。。"))
        }
        val adapter = TestAdapter()
        adapter.data = data
        adapter.addLayout(0,R.layout.item_info)
        mBinding.rv.layoutManager  = LinearLayoutManager(activity)
        mBinding.rv.addItemDecoration(DividerItemDecoration(activity, DividerItemDecoration.VERTICAL))
        mBinding.rv.adapter =adapter
    }
}

class TestAdapter : AbsAdapter<TestData>()

class TestData(val msg: String) : IData {

    override fun bind(holder: RecyclerView.ViewHolder) {
        val binding = ItemInfoBinding.bind(holder.itemView)
        binding.tv.text = msg
    }

}

class Test2Fragment : Fragment() {
    private lateinit var mBinding: FragmentTest2Binding

    val tabs = listOf("2021年", "2020年", "2019年", "2018年", "2017年", "2016年")
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentTest2Binding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.viewpager2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = tabs.size

            override fun createFragment(position: Int): Fragment {
                return TestFragment().apply {
                    val bundle = Bundle()
                    bundle.putInt("page", position)
                    arguments = bundle
                }
            }

        }

        TabLayoutMediator(mBinding.tab2, mBinding.viewpager2) { tab, position ->
            tab.text = tabs[position]
        }.attach()
    }
}